package com.kaishengit.web;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kaishengit.entity.Idea;
import com.kaishengit.entity.IdeaComment;
import com.kaishengit.entity.NewIdea;
import com.kaishengit.entity.User;
import com.kaishengit.entity.UserPro;
import com.kaishengit.service.UserService;
import com.kaishengit.service.IdeaCommentService;
import com.kaishengit.service.IdeaService;
import com.kaishengit.util.GetTime;
import com.kaishengit.util.OutTime;
import com.kaishengit.util.Page;
import com.kaishengit.util.PkTime;
import com.kaishengit.util.PkUuid;
import com.kaishengit.util.TimeUtil;

public class IdeaServlet extends HttpServlet {

	private static final long serialVersionUID = 1;
			
			protected void service(HttpServletRequest request, HttpServletResponse response)
					throws ServletException, IOException {
				String method=request.getParameter("m");
				if(method==null||"".equals(method)){
					idea(request,response);
				}else if("add".equals(method)){
					String m=request.getMethod();
					if("GET".equals(m)){
						toaddidea(request,response);
					}else if("POST".equals(m)){
						addidea(request,response);
					}
				}else if("update".equals(method)){
					
					String m=request.getMethod();
					if("GET".equals(m)){
						toupdateidea(request,response);
					}else if("POST".equals(m)){
						updateidea(request,response);
					}
				}else if("del".equals(method)){
					delidea(request,response);
				}
			}

			private void delidea(HttpServletRequest request,
					HttpServletResponse response) throws ServletException, IOException {
				String id=request.getParameter("id");
				IdeaCommentService ics=new IdeaCommentService();
				ics.delByIdeaid(id);
				
				IdeaService service=new IdeaService();
				service.delIdea(id);
				response.sendRedirect("idea.jspx");
				
			}

			private void updateidea(HttpServletRequest request,
					HttpServletResponse response) throws ServletException, IOException {
				String id=request.getParameter("id");
				String title=request.getParameter("title");
				String content=request.getParameter("content");
				
				IdeaService service=new IdeaService();
				service.updateIdea(id, title, content);
				response.sendRedirect("idea.jspx");
			}

			private void toupdateidea(HttpServletRequest request,
					HttpServletResponse response) throws ServletException, IOException {
				String id= request.getParameter("id" );
				IdeaService service=new IdeaService();
				
				Idea idea=service.findById(id);
				IdeaCommentService s=new IdeaCommentService();
				List<IdeaComment> list=s.findBYId(id);
				String ct=idea.getCreateTime();
				PkTime time=new PkTime();
				String t=time.getTime();
				
				
				if(list.size()==0&&t.substring(0,13).equals(ct.substring(0,13))&&(Integer.parseInt(t.substring(14,16))-Integer.parseInt(ct.substring(14,16)))<5){
					request.setAttribute("idea", idea);
					request.getRequestDispatcher("/WEB-INF/views/update_idea.jsp").forward(request, response);
				}else{
					request.getRequestDispatcher("/WEB-INF/views/fail.jsp").forward(request, response);
				}
			}

			private void addidea(HttpServletRequest request,
					HttpServletResponse response) throws ServletException, IOException {
				String title=request.getParameter("title");
				String content=request.getParameter("content");
				HttpSession session=request.getSession();
				User user=(User)session.getAttribute("user");
				String userId=user.getId();
				
				UserPro userpro =(UserPro) session.getAttribute("userpro");        
				String proId = userpro.getProId();
				PkUuid uuid=new PkUuid();
				String id=uuid.getPk();
				PkTime t=new PkTime();
				String ct=t.getTime();
				
				Idea idea=new Idea();
				idea.setId(id);
				idea.setIdeaTitle(title);
				idea.setUserId(userId);
				idea.setContent(content);
				idea.setCreateTime(ct);
				idea.setProjectId(proId);
				
				IdeaService service=new IdeaService();
				service.save(idea);
				response.sendRedirect("idea.jspx");
				
			}

			private void toaddidea(HttpServletRequest request,
					HttpServletResponse response) throws ServletException, IOException {
				request.getRequestDispatcher("/WEB-INF/views/new_idea.jsp").forward(request, response);
				
			}

			private void idea(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				
			
				String p=request.getParameter("p");
				int pageNum=0;
				if(p==null||"".equals(p)){
					pageNum=1;
				}else{
					pageNum=Integer.parseInt(p);
				}
				
				IdeaService service=new IdeaService();
				
				Page<Idea> page=service.findAll(pageNum);
				List<Idea> list=page.getResult();
				List<NewIdea> newlist=new ArrayList<NewIdea>();
				for(Idea i:list){
					
					NewIdea newidea=new NewIdea();
					newidea.setId(i.getId());
					String id=i.getUserId();
					UserService s=new UserService();
					newidea.setUserName(s.getname(id).getUsername());
					String con=i.getContent();
					newidea.setContent(con);
					newidea.setTitle(i.getIdeaTitle());
					String ct=i.getCreateTime();
					
					OutTime t=new OutTime();
					String createtime=t.getTime(ct);
					newidea.setCreateTime(createtime);
					newlist.add(newidea);
				}
				page.setResult(newlist);
				request.setAttribute("page", page);
				request.getRequestDispatcher("/WEB-INF/views/idea.jsp").forward(request, response);
				
			}
			
}
